﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace SDS.SPHealthAnalyzerRules.Delegation.Helpers
{
    public static class SharePoint
    {
        public static bool isRunning<TService>(this SPServer Server) where TService : SPService
        {
            return Server.ServiceInstances
                         .Where(si => si.Service is TService)
                         .Any();
        }


        public static IEnumerable<SPWebApplication> WebApplications(this SPServer Server)
        {
            return Server.ServiceInstances
                            .OfType<SPWebServiceInstance>()
                            .Select(wsi => wsi.Service as SPWebService)
                            .SelectMany(ws => ws.WebApplications);
        }

        public static IEnumerable<SPWebApplication> UsingKerberos(this IEnumerable<SPWebApplication> WebApps)
        {
            return WebApps.Where(WebApp => WebApp.IisSettings.UsingKerberos().Any());
        }

        public static Dictionary<SPUrlZone, SPIisSettings> UsingKerberos(this Dictionary<SPUrlZone, SPIisSettings> IisSettings)
        {
            return IisSettings.Where(i => !i.Value.DisableKerberos)
                                .ToDictionary(i => i.Key, i => i.Value);
        }


        /// <summary>
        /// Returns a list of the Windows Authentication providers in use for the WebApplication, from the IIS metabase (config file)
        /// </summary>
        public static IEnumerable<string> WindowsAuthenticationProviders(this SPWebApplication WebApp)
        {
            // loads the Windows Authentication provider configuration from the IIS metabase/config file
            using (var ServerManager = new Microsoft.Web.Administration.ServerManager())
            {
                return ServerManager
                        .GetApplicationHostConfiguration()
                        .GetSection("system.webServer/security/authentication/windowsAuthentication", WebApp.DisplayName)
                        .GetCollection("providers")
                        .Select(prov => prov.Attributes["value"].Value as string);
            } // using ServerManager

        }

        /// <summary>
        /// Returns a list of the Windows Authentication providers in use for the WebApplication, from the IIS metabase (config file)
        /// </summary>
        public static bool WindowsAuthentication_usingKernelMode(this SPWebApplication WebApp)
        {
            // loads the Windows Authentication provider configuration from the IIS metabase/config file
            using (var ServerManager = new Microsoft.Web.Administration.ServerManager())
            {
                var UsingKernelMode = ServerManager
                                            .GetApplicationHostConfiguration()
                                            .GetSection("system.webServer/security/authentication/windowsAuthentication", WebApp.DisplayName)
                                            .Attributes["useKernelMode"].Value as string;

                return Boolean.Parse(UsingKernelMode);
            }
        }

    }
}
